
public var _xIndex:int;
public var _zIndex:int;
private var _height:int;
private var _costToMove:float = 1f;
public var _unfogCount:int = 0;
public var _isObstacle:boolean = false;
public var _isOccupied:boolean = false;
public var _isCover:boolean = false;
public var _coverDirectionTop:boolean = false;
public var _coverDirectionRight:boolean = false;
public var _coverDirectionBottom:boolean = false;
public var _coverDirectionLeft:boolean = false;

private var _oldCoverDirectionTop:boolean = false;
private var _oldCoverDirectionRight:boolean = false;
private var _oldCoverDirectionBottom:boolean = false;
private var _oldCoverDirectionLeft:boolean = false;
private var _oldIsCover:boolean = false;


//Obstacle reference
private var _obstacle:Obstacle = null;
//character reference
private var _character:GameCharacter = null;
//enemy reference
private var _enemy:EnemyCharacter = null;
//Neighbour tile reference
private var _neighbourTiles:Array = new Array(8);
//Map reference
private var _map:Map = null;

public static var STATE_FOG:int = 1;
public static var STATE_NORMAL:int = 2;
public static var STATE_HIGHLIGHT:int = 3;

public static var COVER_DIRECTION_TOP:int = 1;
public static var COVER_DIRECTION_RIGHT:int = 2;
public static var COVER_DIRECTION_BOTTOM:int = 3;
public static var COVER_DIRECTION_LEFT:int = 4;

public var currentState = STATE_NORMAL;

private var TILE_HIGHLIGHT = Color.yellow;
private var TILE_FOG = Color.grey;
private var TILE_NORMAL = Color.white;

//For exposed movement
public var _isMovingOver:boolean = false;

public function initialize(x:int, z:int, height:int):void
{
	_xIndex = x;
	_zIndex = z;
	_height = height;
	for (var i = 0; i < _neighbourTiles.length; i++)
		_neighbourTiles[i] = null;
}

public function setMapRef(map:Map)
{
	_map = map;
}
public function setTexture(t:Texture):void
{
	this.renderer.material.mainTexture = t;
}


public function getCoord():Vector2{ return new Vector2(_xIndex, _zIndex); }
public function getWorldCoord():Vector3 { return this.transform.position; }
public function getSurfaceCoord():Vector3
{
	var v:Vector3 = getWorldCoord();
	v.y += this.renderer.bounds.size.y/2 +0.001;
	return v;
}

public function setHeight(height:int):void 
{ 
	_height = height; 
	if (this != null)
	{
		this.transform.position.y = this.renderer.bounds.size.y/3 * height;
		originalHeight = this.transform.position;
	}
}
public function getHeight():int { return _height; }

public function setCostToMove(cost:float):void { _costToMove = cost; }
public function getCostToMove():float { return _costToMove; }

public function setIsObstacle(b:boolean):void { _isObstacle = b; }
public function isObstacle():boolean { return _isObstacle; }

public function setIsOccupied(b:boolean):void { _isOccupied = b; }
public function isOccupied():boolean { return _isOccupied; }

public function setObstacle(obstacle:Obstacle):void { _obstacle = obstacle; }
public function getObstacle():Obstacle { return _obstacle; }

public function setIsCover(b:boolean):void { _isCover = b; }
public function isCover():boolean { return _isCover; }

public function setIsMovingOver(b:boolean):void { _isMovingOver = b; }
public function isMovingOver():boolean { return _isMovingOver; }

public function setCoverDirection(direction:int):void 
{
	switch ( direction)
	{
		case COVER_DIRECTION_TOP:
			_coverDirectionTop = true;
			break;
		case COVER_DIRECTION_RIGHT:
			_coverDirectionRight = true;
			break;
		case COVER_DIRECTION_BOTTOM:
			_coverDirectionBottom = true;
			break;
		case COVER_DIRECTION_LEFT:
			_coverDirectionLeft = true;
			break;
	}
	setIsCover(true);
}
public function getCoverDirection(direction:int):boolean
{
	switch (direction)
	{
		case COVER_DIRECTION_TOP:
			return _coverDirectionTop;
		case COVER_DIRECTION_RIGHT:
			return _coverDirectionRight;
		case COVER_DIRECTION_BOTTOM:
			return _coverDirectionBottom;
		case COVER_DIRECTION_LEFT:
			return _coverDirectionLeft;
	}
}

public function saveCoverData():void
{
	_oldCoverDirectionTop = _coverDirectionTop;
	_oldCoverDirectionRight = _coverDirectionRight;
	_oldCoverDirectionBottom = _coverDirectionBottom;
	_oldCoverDirectionLeft = _coverDirectionLeft;
	_oldIsCover = _isCover;
}

public function loadCoverData():void
{
	_coverDirectionTop = _oldCoverDirectionTop;
	_coverDirectionRight = _oldCoverDirectionRight;
	_coverDirectionBottom = _oldCoverDirectionBottom;
	_coverDirectionLeft = _oldCoverDirectionLeft;
	_isCover = _oldIsCover;
}

public function manualSetObstacle(manualSet:boolean)
{
	if (manualSet)
	{
		//convert this tile to be a fake obstacle
		_isObstacle = true;
		_isCover = true;
		
		_coverDirectionTop = true;
		_coverDirectionRight = true;
		_coverDirectionBottom = true;
		_coverDirectionLeft = true;
		
		//update surrounding tiles
		_neighbourTiles[0].setCoverDirection(COVER_DIRECTION_BOTTOM);
		
		_neighbourTiles[2].setCoverDirection(COVER_DIRECTION_LEFT);
		
		_neighbourTiles[4].setCoverDirection(COVER_DIRECTION_TOP);
		
		_neighbourTiles[6].setCoverDirection(COVER_DIRECTION_RIGHT);
		
		
	}
	else
	{	
		_isObstacle = false;
		this.loadCoverData();
		_neighbourTiles[0].loadCoverData();
		_neighbourTiles[2].loadCoverData();
		_neighbourTiles[4].loadCoverData();
		_neighbourTiles[6].loadCoverData();
		//load back previous settings
	}
	
}

public function setCharacter(character:GameCharacter):void
{
	_character = character;
}
public function getCharacter():GameCharacter { return _character; }

public function setEnemy(enemy:EnemyCharacter):void
{
	_enemy = enemy;
}
public function getEnemy():EnemyCharacter{ return _enemy; }


public function clearCharacter():void
{
	_character = null;
	_isOccupied = false;
}

public function clearEnemy():void
{
	_enemy = null;
	_isOccupied = false;
}

public function clearObstacle():void
{
	_isObstacle = false;
	_obstacleModel = null;
}

public function setNeighbourTile(tileReference:Tile, index:int)
{
	if (index >= 0 && index < 8)
		_neighbourTiles[index] = tileReference;
}

public function getNeighbourTiles():Array { return _neighbourTiles; }

public function getTileState() { return currentState;}
public function setTileState(state:int):void
{
	//from fogged to unfogged
	if (currentState == STATE_FOG && state == STATE_NORMAL)
		_unfogCount++;
	//from unfogged to unfogged
	else if (currentState == STATE_NORMAL && state == STATE_NORMAL)
		_unfogCount++;
	//from unfogged to fogged
	else if (currentState == STATE_NORMAL && state == STATE_FOG){
		_unfogCount--;
	}
	
	if (currentState == state)
		return;
	switch(state)
	{	
		case STATE_FOG:
			if (_unfogCount <= 0)
			{
				//hide all things in the fog
				if (_obstacle != null)
					_obstacle.setIsVisible(false);
				if (_enemy != null)
					_enemy.setIsVisible(false);
				this.renderer.material.color = TILE_FOG;
				
				_unfogCount = 0;
				currentState = state;
			}
			break;
		
		case STATE_NORMAL:
			//show all things
			if (_obstacle != null)
				_obstacle.setIsVisible(true);
				
			if (_enemy != null)
				_enemy.setIsVisible(true);
			this.renderer.material.color = TILE_NORMAL;
			currentState = state;
			break;
		
		case STATE_HIGHLIGHT:
			this.renderer.material.color = TILE_HIGHLIGHT;
			currentState = state;
			break;
	}
	
	
	
			

}

function OnMouseEnter()
{
	_map.setMouseOverTile(this);
}

function OnMouseDown()
{
	_map.setSelectedTile(this);
}

public function highlightMe()
{
	OnMouseEnter();
}

public function ToString():String {
	return "Tile: [" + _xIndex + "," + _zIndex + "]";
}

public function getIsVisible()
{
	if (currentState == STATE_NORMAL)
		return true;
	if (currentState == STATE_FOG)
		return false;
}

